<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopSpecialProduct {
	var $db;
 	var $smarty;
 	
    public function __construct($db, $smarty) {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
    
    public function CreateSpecialProduct() {
		$expires_date = "";
		$status = "";
		$product_id = $_POST['product_id'];
		$currencies_id = BWebshopProductBean::getCurrencyByProductId($this->db, $product_id);
		$specials_new_products_price = @$_POST['specials_new_products_price'];
		if(isset($_POST['expires_date']))
			$expires_date = @$_POST['expires_date'];
		if(isset($_POST['status']))
			$status = @$_POST['status'];
		$listspecial = BWebshopSpecialProductBean::getAllSpecials($this->db);
		$position = count($listspecial)+1;	
		$type = array('product_id' => $product_id,
					  'specials_new_products_price' => $specials_new_products_price,
					  'expires_date' => $expires_date,
					  'position' => $position,
					   'status' => $status,
					  'currency_id' => $currencies_id);
		$ret = "";				 
		$ret = BWebshopSpecialProductBean::validateSpecial($this->db, $type);
		
		if($ret == "") {					
			$id = BWebshopSpecialProductBean::saveSpecial($this->db, $type);
			if($id > 0) {
				$type['ID'] = $id;
				$type['message'] = "New Special has been added.";
				header("Location: ./?module=BgcmsWebshop&sub1=specials");
				return;
			} else {
				$type['message'] = "Could not insert new Special to database.";
			}
		} else {
			$type = array('ID' => -1, 'message' => $ret);
		}
		$product = BWebshopProductBean::getProduct($this->db, $product_id);
		$product_name = $product -> product_name;
		$product_price = $product -> product_price;
		$symbol_left = $product -> symbol_left;
		$type['product_name'] = $product_name;
		$type['product_price'] = $product_price;
		$type['symbol_left'] = $symbol_left;
		//echo array2json($type);		
	}
	
	public function DeleteSpecialProduct() {
		$ID = $_POST['id'];
		$old_pos = $_POST['position'];
			$listspecial = BWebshopSpecialProductBean::get_special_by_level($this->db,$ID);	
			
			if($listspecial!="") {
				foreach($listspecial as $item) {
					if($item->position>$old_pos) {
						$new_position = $item->position -1;
						BWebshopSpecialProductBean::update_position_special($this->db,$item->special_id,$new_position);
					}
				}	
			}	
	
		$arr = array("code" => -1, "message" => _("Invalid Arguments."));
		if(isset( $_POST['id'])) {
			$res = BWebshopSpecialProductBean::deleteSpecial($this->db, $_POST['id']);
			if($res) {
				$arr['code'] = 0;
				$arr['message'] = sprintf(_("Special with Id %s has been deleted."), "<b>". $_POST['id'] . "</b>");
			} else {
				$arr['message'] = _("Could not delete Special, Id: ") . $_POST['id'];
			}
		}
				
		echo array2json($arr);
		//we use ajax delete product, so no need to show rest of page here
	}
	
	public function EditSpecialProduct() {
		if(isset( $_POST['id'])) {
			$special_id = @$_POST['id'];
			$expires_date = "";
			$status = @$_POST['status'];
			$product_id = @$_POST['product_id'];
			$currencies_id = BWebshopProductBean::getCurrencyByProductId($this->db, $product_id);
			$specials_new_products_price = @$_POST['specials_new_products_price'];
			if(isset($_POST['expires_date']))
				$expires_date = @$_POST['expires_date'];
				
			$dbstatus = BWebshopSpecialProductBean::getSpecialStatusById($this->db, $special_id);
			if($status != $dbstatus->status) {
				$date_status_change = date("Y-m-d H:i:s");
			}
			if($status == $dbstatus->status) {
				$date_status_change = $dbstatus->date_status_change;
			}
			$type = array('special_id' => $special_id,
						  'product_id' => $product_id,
					      'specials_new_products_price' => $specials_new_products_price,
					  	  'expires_date' => $expires_date,
					  	  'date_status_change' => $date_status_change,
					  	  'status' => $status,
					  	  'currency_id' => $currencies_id);
			$msgId = 2;
			$msg = "";				 
			$msg = BWebshopSpecialProductBean::validateSpecial($this->db, $type);
			if($msg == "") {
				$res = BWebshopSpecialProductBean::updateSpecial($this->db, $type);	
				if($res>0) {
					$msgId = 1;
					header("Location: ./?module=BgcmsWebshop&sub1=specials&message=" . $msgId);
					return;
				}
			} else {
				//we set msgId to 99 to indicate message will load in session variable
				$msgId = 99;
				setcookie("message", $msg,time()+5);
			}
			header("Location: ./?module=BgcmsWebshop&sub1=editspecial&message=" . $msgId . "&id=" . $special_id);
		}
	}
	
	public function change_position() {
		$old_pos = $_POST['position'];
		$special_id = $_POST['special_id'];
		
		$listspecial = BWebshopSpecialProductBean::get_special_by_level($this->db,$special_id);
		if($_POST['type_change'] == 'first') {
			foreach($listspecial as $item) {
				if($item->position<$old_pos) {
					$new_position = $item->position +1;
					BWebshopSpecialProductBean::update_position_special($this->db,$item->special_id,$new_position);
				}
			}	
			BWebshopSpecialProductBean::update_position_special($this->db,$special_id,'1');	
		}
		else if($_POST['type_change'] == 'last') {
			foreach($listspecial as $item) {
				if($item->position>$old_pos) {
					$new_position = $item->position-1;
					BWebshopSpecialProductBean::update_position_special($this->db,$item->special_id,$new_position);
				}
			}
			$newpos = count($listspecial)+1;
			BWebshopSpecialProductBean::update_position_special($this->db,$special_id,$newpos);				
		}
		else if($_POST['type_change'] == 'after') {
			$after_id = $_POST['position_id'];
			foreach($listspecial as $item) {
				if($old_pos>$after_id) {
					if($item->position>$after_id && $item->position<$old_pos) {
						$new_position = $item->position+1;
						BWebshopSpecialProductBean::update_position_special($this->db,$item->special_id,$new_position);	
					}
				} else {
					if($item->position<=$after_id && $item->position>$old_pos) {
						$new_position = $item->position-1;
						BWebshopSpecialProductBean::update_position_special($this->db,$item->special_id,$new_position);	
					}
				}	
			}	
			if($old_pos>$after_id)
				BWebshopSpecialProductBean::update_position_special($this->db,$special_id,$after_id+1);
			else
				BWebshopSpecialProductBean::update_position_special($this->db,$special_id,$after_id);
	
		}
		//Back URL after change position
		header("Location: ./?module=BgcmsWebshop&sub1=specials");
	}
}
?>